﻿using DAL.Entities;
using Dapper;
using Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.FKTZ
{
    public class FktzDAL
    {
        private readonly FktzDataContext _dataContext;
        public FktzDAL(FktzDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public  Task<IEnumerable<Fktz>> ListAsync(string deptid, string name, DateTime? startTime, DateTime? endTime, int pageSize, int pageIndex)
        {
            var query = _dataContext.Fktzs.AsNoTracking().AsQueryable();
            if (!string.IsNullOrEmpty(deptid)) query = query.Where(x => x.DeptId == deptid);
            if (!string.IsNullOrEmpty(name)) query = query.Where(x => x.UserName.Contains(name)||x.Cause.Contains(name));
            if (startTime!=null&&endTime != null)
            {
                DateTime endTimeAdd = Convert.ToDateTime(endTime).AddDays(1);
                query = query.Where(x => x.AccessTime >= startTime && x.AccessTime <=endTimeAdd);
            }
            else if (startTime != null)
            {
                query = query.Where(x => x.AccessTime >= startTime);
            }
            else if(endTime != null)
            {
                DateTime endTimeAdd = Convert.ToDateTime(endTime).AddDays(1);
                query = query.Where(x => x.AccessTime <endTimeAdd);
            }
            return Task.FromResult(query.OrderByDescending(x => x.AccessTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList() as IEnumerable<Fktz>);


        }
        public async Task<IEnumerable<Fktz>> ListAsync()
        {
            return await (from q in _dataContext.Fktzs
                          select q).ToListAsync();

        }
        public async Task<Fktz> GetAsync(string id)
        {
            return await _dataContext.Fktzs.FindAsync(id);
        }

        public async Task CreateAsync(Fktz entity)
        {
            _dataContext.Fktzs.Add(entity);
        }
        public async Task CreateAsync(IEnumerable<FktzAttachment> entities)
        {
            foreach (var item in entities)
            {
                await CreateAsync(item);
            }
        }
        public async Task CreateAsync(FktzAttachment entity)
        {
            _dataContext.Entry(entity).State = EntityState.Added;
        }
        public async Task UpdateAsync(Fktz entity)
        {
            _dataContext.Entry(entity).State = EntityState.Modified;
        }

        public async Task SaveChangeAsync()
        {
            _dataContext.SaveChangesAsync();
        }

        public async Task DeleteAsync(string id)
        {
            var entity = await GetAsync(id);
            _dataContext.Entry(entity).State = EntityState.Deleted;
        }
        private SqlDbHelper sqldbhelper = new SqlDbHelper();
        public string  Save(FktzModel entity)
        {

            string sql = string.Empty;
            string returnVal="-1";
            try
            {
                using (IDbConnection connect = new SqlConnection(sqldbhelper.strCon))
                {
                    //if (string.IsNullOrEmpty(entity.FktzId))
                    //{
                        //    entity.FktzId = Guid.NewGuid().ToString();
                        //    returnVal = connect.Execute("insert into fktz(FktzId, EntryName, UserName, ContactInformation, IDNumber, VisitinUnit, Cause, AccessTime, Temperature, Remarks)values" +
                        //        "('"+ entity.FktzId + "', '" + entity.EntryName + "', '" + entity.UserName + "', '" + entity.ContactInformation + "', '" + entity.IDNumber + "', '" + entity.VisitinUnit + "', '" + entity.Cause + "', '" + entity.AccessTime + "', " + entity.Temperature + ", '" + entity.Remarks + "')").ToString();
                        //}
                        sql = @"insert into fktz(FktzId,EntryName,UserName,ContactInformation,UserIDNumber,VisitinUnit,Cause,AccessTime,Temperature,Remarks) 
                                 values(@fktzId,@EntryName,@UserName,@ContactInformation,@UserIDNumber,@VisitinUnit,@Cause,@AccessTime,@Temperature,@Remarks)";

                        SqlParameter[] param = new SqlParameter[] {
                    new SqlParameter("@fktzId",entity.FktzId),
                    new SqlParameter("@EntryName",entity.EntryName),
                     new SqlParameter("@UserName",entity.UserName),
                    new SqlParameter("@ContactInformation",entity.ContactInformation),
                    new SqlParameter("@UserIDNumber",entity.UserIDNumber),
                    new SqlParameter("@VisitinUnit",entity.VisitinUnit),
                    new SqlParameter("@Cause",entity.Cause),
                    new SqlParameter("@AccessTime",entity.AccessTime),
                    new SqlParameter("@Temperature",entity.Temperature),
                    new SqlParameter("@Remarks",entity.Remarks)

                    };
                    returnVal = connect.Execute(sql, param).ToString();

                }
            }
            catch (Exception e)
            {
                return entity.FktzId + e.ToString();
            }
            return returnVal;
        }
    }
}
